package com.gcstorage.tvdev.base;

import android.app.Application;
import android.content.res.Resources;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.launcher.ARouter;

/**
 * @author: liangkun
 * @date: 2022/3/30
 * @des:
 */
public abstract class BaseActivity extends RxBaseActivity {
    protected BaseViewModel viewModel;
    protected ViewModelProvider viewModelProvider;
    protected ViewModelProvider.Factory factory;

    protected BaseViewModel createViewModel() {
        return null;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        viewModel = createViewModel();
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        observeLiveData();

    }

    protected void observeLiveData() {
        if (viewModel != null) {
            viewModel.loadingShowLD.observe(this, this::showLoadingDialog);
        }
    }


    protected ViewModelProvider getViewModelProvider() {
        if (viewModelProvider == null) {
            viewModelProvider = new ViewModelProvider(this, getFactory());
        }
        return viewModelProvider;
    }

    private ViewModelProvider.Factory getFactory() {
        Application application = getApplication();
        if (application == null) {
            throw new IllegalStateException("Your activity/fragment is not yet attached to "
                    + "Application. You can't request ViewModel before onCreate call.");
        }
        if (factory == null) {
            factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application);
        }
        return factory;
    }

    /**
     * 重写 getResource 方法，防止系统字体影响
     */
    @Override
    public Resources getResources() {//禁止app字体大小跟随系统字体大小调节
        Resources resources = super.getResources();
        final float commonScale = 1.0F;
        if (resources != null && resources.getConfiguration().fontScale != commonScale) {
            android.content.res.Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }


}
